package com.nidu.demo.dict.executor.query;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.dict.convertor.DictTypeCriteriaConvertor;
import com.nidu.demo.dict.dto.DictTypeListQry;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.dict.convertor.DictTypeConvertor;
import com.nidu.demo.dict.dto.DictTypeCO;
import com.nidu.demo.dict.gateway.DictTypeGateway;
import com.nidu.demo.dict.model.DictType;
import com.nidu.demo.dict.model.DictTypeCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 字典类型列表查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class DictTypeListQryExe {

    private final DictTypeGateway gateway;

    private final DictTypeConvertor convertor;

    private final DictTypeCriteriaConvertor criteriaConvertor;

    public MultiResponse<DictTypeCO> execute(DictTypeListQry qry) {
        DictTypeCriteria criteria = criteriaConvertor.toDataObject(qry);
        List<DictType> dictTypes = gateway.listByCondition(criteria);
        if(CollUtil.isEmpty(dictTypes)){
            throw new BizException(ErrorCodeConstants.DICT_TYPE_NOT_FOUND.getCode(), ErrorCodeConstants.DICT_TYPE_NOT_FOUND.getMessage());
        }
        List<DictTypeCO> companyCOS = convertor.toClientObjects(dictTypes);
        return MultiResponse.of(companyCOS);
    }

}